package com.example.easyexceldemo.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import com.example.easyexceldemo.model.Relation;
import com.example.easyexceldemo.model.Student;
import com.example.easyexceldemo.util.ListUtil;

/**
 * 模拟服务
 */
@Service
public class StudentService {

	//全量查询数据库
	public List<Student> getAll() {
		List<Student> list = new ArrayList<>();
		for (long i = 1; i <= 100000; i++) {
			Student stu = new Student();
			stu.setId(i);
			stu.setAge(RandomUtils.nextInt(8, 26));
			stu.setStartTime(new Date());
			stu.setEndTime(DateUtils.addDays(new Date(), 3));
			stu.setName("张" + i);
			stu.setSalary(new BigDecimal(String.valueOf(RandomUtils.nextDouble(666d, 10000.99d))));
			stu.setType(RandomUtils.nextInt(0, 3));
			stu.setTeacher("王" + i);
			stu.setRelations(getRelations(stu));
			list.add(stu);
		}
		return list;
	}

	//分页查询数据库
	public List<Student> page(Integer pageNo, Integer pageSize) {
		List<Student> all = this.getAll();
		List<List<Student>> paging = ListUtil.paging(all, pageSize);
		if (paging.size() > 0 && paging.size() >= pageNo) {
			List<Student> list = paging.get((int) (pageNo - 1));
			return list;
		}
		return Collections.emptyList();
	}

	private List<Relation> getRelations(Student stu) {
		List<Relation> list = new ArrayList<>();
		if (stu.getAge() > 22) {
			Relation r1 = new Relation();
			r1.setName("小棉袄");
			list.add(r1);

			Relation r2 = new Relation();
			r2.setName("老婆");
			list.add(r2);
		} else {
			Relation r1 = new Relation();
			r1.setName("好基友");
			list.add(r1);

			Relation r2 = new Relation();
			r2.setName("女盆友");
			list.add(r2);
		}
		return list;
	}

	public String findByTeacherName(String teacher) {
		if(teacher!=null && teacher.startsWith("王")) {
			String str = teacher.substring(1);
			return "NO"+str;
		}
		return null;
	}

}
